//
//  PlayerItem.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/4/13.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "PlayerItem.h"
#import "ScenicPlayVC.h"

@implementation PlayerItem

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _iconImageView = [[UIImageView alloc] init];
       
        _iconImageView.bounds = CGRectMake(0, 0, 100, 100);
        _iconImageView.layer.position = CGPointMake(0.5,0.5);
        _iconImageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_iconImageView];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(PlayerItemChangePlayStateWith:) name:kPlayManagerState object:nil];
    }
    return self;
}


- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)tap:(UITapGestureRecognizer *)tap
{
    ScenicPlayVC *qqOnlineVC = [[ScenicPlayVC alloc] init];
    qqOnlineVC.pressentType = TransitionPressentTypeMask;
    qqOnlineVC.CurrentPointID = self.currenPlayID;
    
    [[IGUtils currentViewController] presentViewController:qqOnlineVC animated:YES completion:nil];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _iconImageView.frame = CGRectMake((self.frame.size.width - 44)/2, (self.frame.size.width - 44)/2, 44, 44);
    
}
- (void)pan:(UIPanGestureRecognizer *)pan
{
    CGPoint point = [pan locationInView:[UIApplication sharedApplication].keyWindow];
    PlayerItem *view = (PlayerItem *)pan.view;
    
    CGFloat distance = 40;  // 离四周的最小边距
    
    if (pan.state == UIGestureRecognizerStateEnded) {
        
        
        if (point.y <= distance) {
            
            point.y = distance;
            
        }else if (point.y >= [UIScreen mainScreen].bounds.size.height - distance){
            
            point.y = [UIScreen mainScreen].bounds.size.height - distance;
        }else if (point.x <= [UIScreen mainScreen].bounds.size.width/2.0) {
            
            point.x = distance;
            
        }else {
            
            point.x = [UIScreen mainScreen].bounds.size.width - distance;
        }
        
        [UIView animateWithDuration:0.5 animations:^{
            view.center = point;
            
        }];
        
    }else {
        
        view.center = point;
    }
   
}

- (void)setIsItemPlay:(BOOL)isItemPlay {
    NSString * iconStr = isItemPlay ?  @"play":@"stop";
    self.iconImageView.image = [UIImage imageNamed:iconStr];
}
- (void)PlayerItemChangePlayStateWith:(NSNotification *)notif
{
    NSInteger state = [notif.userInfo[kPlayManagerState] integerValue];
    switch (state) {
        case AVPlayerPlayStatePreparing:
        case AVPlayerPlayStateBeigin:
        case AVPlayerPlayStatePlaying:
        {
            self.isItemPlay = YES;
        }
            break;
        case AVPlayerPlayStatePause:
        case AVPlayerPlayStateEnd:
        case AVPlayerPlayStateNotPlay:
        case AVPlayerPlayStateNotKnow:
        {
            self.isItemPlay = NO;
        }
            break;
        default:
            break;
    }
    
}

@end
